Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASGI & WSGI instrument methods #324

Merged
merged 11 commits into from
Oct 30, 2024
Merged

Add ASGI & WSGI instrument methods #324

merged 11 commits into from
Oct 30, 2024

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Jul 19, 2024

Is this what you had in mind? @alexmojaki

I don't like it.

Copy link

cloudflare-workers-and-pages bot commented Jul 19, 2024

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 389c912
Status: ✅  Deploy successful!
Preview URL: https://4fbd4907.logfire-docs.pages.dev
Branch Preview URL: https://asgi-wsgi.logfire-docs.pages.dev

View logs

@alexmojaki
Copy link
Contributor

Pretty much. What would you prefer? Just a different name?

@Kludex
Copy link
Member Author

Kludex commented Jul 19, 2024

I don't like that every instrument_ method is returns None, but those 2 here need to return the app itself.

@alexmojaki
Copy link
Contributor

I agree with that. The difference is more obvious in OTEL because it's not e.g. ASGIInstrumentor().instrument(app) like other things. We could maybe have instrumented_asgi but people might not notice the difference. If people copy the code from the docs then they'll probably get it right, but they might not do that.

If it was name logfire.asgi_middleware(app) or something else which didn't start with instrument_, but the rest was the same, how would you feel?

@Kludex
Copy link
Member Author

Kludex commented Jul 19, 2024

If it was name logfire.asgi_middleware(app) or something else which didn't start with instrument_, but the rest was the same, how would you feel?

I think I'll prefer more if it was a class itself e.g. logfire.ASGIMiddleware(). Because then it's clear for those familiar with ASGI that it returns an ASGI application.

@alexmojaki
Copy link
Contributor

But an ASGI application is just a callable, often a function.

@Kludex
Copy link
Member Author

Kludex commented Jul 19, 2024

It's a callable, but I don't think it's usually a function. In any case, I think it's irrelevant if an app itself is usually a function or a class, but ASGI middlewsres in the ecosystem are usually presented as classes.

@alexmojaki
Copy link
Contributor

Currently you can do this:

import logfire

logfire_instance = logfire.with_tags('foo')

logfire.instrument_bar()
# or
logfire_instance.instrument_bar()

Right now that's not very useful, but we may add other logfire.with_* methods. More importantly I want to eventually implement #195 which would allow logfire_instance = logfire.configure(..., global=False).

I want the instrument methods to be instances of the Logfire class so that they all support logfire_instance.instrument_bar(). If logfire.ASGIMiddleware is a class then logfire_instance.ASGIMiddleware will be an error, and the user will have to write app = logfire.ASGIMiddleware(app, logfire_instance=logfire_instance). That feels a bit sad. BUT maybe it's fine if we're already giving up on a name like logfire.instrument_asgi anyway. WDYT?

@Kludex
Copy link
Member Author

Kludex commented Jul 21, 2024

I want the instrument methods to be instances of the Logfire class so that they all support logfire_instance.instrument_bar(). If logfire.ASGIMiddleware is a class then logfire_instance.ASGIMiddleware will be an error, and the user will have to write app = logfire.ASGIMiddleware(app, logfire_instance=logfire_instance). That feels a bit sad. BUT maybe it's fine if we're already giving up on a name like logfire.instrument_asgi anyway. WDYT?

Ah, I see. Then I think we can just have it as functions.

@alexmojaki
Copy link
Contributor

Do you mean methods of the Logfire class?

@Kludex
Copy link
Member Author

Kludex commented Jul 29, 2024

Do you mean methods of the Logfire class?

Yes.

@Kludex Kludex self-assigned this Jul 29, 2024
@sydney-runkle
Copy link
Member

@Kludex, ping me when this is ready for review, I'd love to take a look.

@Kludex
Copy link
Member Author

Kludex commented Oct 29, 2024

I'll work on this today.

Copy link

codecov bot commented Oct 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4615e9d) to head (389c912).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #324   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          132       133    +1     
  Lines        10175     10206   +31     
  Branches      1395      1395           
=========================================
+ Hits         10175     10206   +31     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Kludex Kludex requested a review from alexmojaki October 29, 2024 15:32
docs/integrations/asgi.md Outdated Show resolved Hide resolved
docs/integrations/asgi.md Show resolved Hide resolved
logfire/_internal/main.py Outdated Show resolved Hide resolved
logfire/_internal/main.py Show resolved Hide resolved
logfire/_internal/main.py Outdated Show resolved Hide resolved
docs/integrations/wsgi.md Show resolved Hide resolved
logfire/_internal/integrations/asgi.py Show resolved Hide resolved
logfire/_internal/main.py Outdated Show resolved Hide resolved
You can read more about the OpenTelemetry ASGI middleware [here][opentelemetry-asgi].
The keyword arguments of [`logfire.instrument_asgi()`][logfire.Logfire.instrument_asgi] are passed to the
[`OpenTelemetryMiddleware`][opentelemetry.instrumentation.asgi.OpenTelemetryMiddleware] class
of the OpenTelemetry ASGI Instrumentation package.

## Excluding URLs from instrumentation
<!-- note that this section is duplicated for different frameworks but with slightly different links -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The note here should refer to instrument_asgi, not OpenTelemetryMiddleware.

In fact we should probably just implement the env var ourselves.

docs/integrations/wsgi.md Outdated Show resolved Hide resolved
@Kludex Kludex requested a review from alexmojaki October 30, 2024 10:50
@Kludex
Copy link
Member Author

Kludex commented Oct 30, 2024

@alexmojaki something else here?

@alexmojaki alexmojaki merged commit e954e2a into main Oct 30, 2024
21 checks passed
@alexmojaki alexmojaki deleted the asgi-wsgi branch October 30, 2024 13:15
@alexmojaki
Copy link
Contributor

Thanks!!! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants